﻿namespace AutoBackupDatabase.Form
{
    using System;
    using System.Windows.Forms;

    using AutoBackupDatabase.BLL;
    using AutoBackupDatabase.ENT;

    using DevExpress.XtraGrid.Columns;

    /// <summary>
    /// The frm permit.
    /// </summary>
    public partial class frmPermit : DevExpress.XtraEditors.XtraForm
    {
        private DETAILGROUPUSERROLEBLL bll;

        private GROUPUSERBLL groupuserbll;
        public frmPermit()
        {
            InitializeComponent();
            bll=new DETAILGROUPUSERROLEBLL();
            groupuserbll = new GROUPUSERBLL();
        }

        private void frmPermit_Load(object sender, EventArgs e)
        {
            var part = groupuserbll.GET_GROUPUSER_ALL();
      
            glkGroupUser.Properties.DisplayMember = "GROUPUSERNAME";
            glkGroupUser.Properties.ValueMember = "GROUPUSERCODE";
            if (!General.general.isAdministrator)
            {
               part.RemoveAt(part.FindIndex(0,x => x.GROUPUSERNAME.ToLower() == "administrator"));
               
            }
            glkGroupUser.Properties.DataSource = part;

            glkGroupUser.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            GridColumn col2 = glkGroupUser.Properties.View.Columns.AddField("GROUPUSERNAME");
            col2.VisibleIndex = 0;
            col2.Caption = "Nhóm người dùng";

           
        }

        private void glkGroupUser_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                gridListRole.DataSource = bll.LOADROLEBYGROUPUSERCODE(int.Parse(glkGroupUser.EditValue.ToString()));
            }
            catch (Exception)
            {
                    
                
            }
        }

        private DETAILGROUPUSERROLEENT detailgroupuserroleent;
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (bll.DELETEDETAILROLEBYGROUPUSERID(int.Parse(glkGroupUser.EditValue.ToString())))
                {
                    if (gridView1.DataRowCount > 0)
                    {
                        for (int i = 0; i < gridView1.DataRowCount; i++)
                        {
                            
                                detailgroupuserroleent = new DETAILGROUPUSERROLEENT();
                                detailgroupuserroleent.GROUPUSERCODE = int.Parse(gridView1.GetRowCellValue(i, "GROUPUSERCODE").ToString());
                                detailgroupuserroleent.ROLECODE =gridView1.GetRowCellValue(i, "ROLECODE").ToString();
                                detailgroupuserroleent.PERMIT = bool.Parse(gridView1.GetRowCellValue(i, "PERMIT").ToString());
                                bll.INSERTDETAILGROUPUSERROLE(detailgroupuserroleent);
                            
                            
                        }
                        MessageBox.Show("Lưu thành công", "Thông báo");
                    }
                    
                }
            }
            catch (Exception)
            {
            }
        }
    }
}